﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using DAL;

namespace BIZ
{
    public class CategoryBIZ : BaseClass
    {
        public void Edit(CATEGORY category)
        {
            var c = (from ct in WebUtils.db.CATEGORies where ct.CategoryID.Equals(category.CategoryID) select ct).SingleOrDefault();
            c.CategoryName = category.CategoryName;

            WebUtils.db.SubmitChanges();
        }

        public CATEGORY GetByID(Guid categoryID)
        {
            var category = (from c in WebUtils.db.CATEGORies where c.CategoryID.Equals(categoryID) select c).FirstOrDefault();
            return category;
        }

        public List<CATEGORY> GetParentNode()
        {
            var listCategory = from c in DB.CATEGORies where c.Parent.HasValue == false orderby c.Position select c;
            return listCategory.ToList();
        }

        public List<CATEGORY> GetChildNode(string id)
        {
            var listCategory = (from c in DB.CATEGORies where c.Parent.Equals(id) orderby c.Position select c).ToList();

            for (int i = 0; i < listCategory.Count; i++)
            {
                listCategory[i].CategoryName = "---" + listCategory[i].CategoryName;
            }

            return listCategory;
        }

        public void Add(CATEGORY category)
        {
            if (category.Parent.HasValue)
            {
                var position = (from p in WebUtils.db.CATEGORies where p.Parent.Value.Equals(category.Parent) select p.Position).Max();
                category.Position = position + 1;
            }
            else
            {
                var position = (from p in WebUtils.db.CATEGORies where p.Parent.HasValue == false select p.Position).Max();
                category.Position = position + 1;
            }

            category.CategoryID = Guid.NewGuid();
            DB.CATEGORies.InsertOnSubmit(category);
            DB.SubmitChanges();

            UrlBIZ.GetAllUrl();
        }


        public bool CheckCategoryCanDel(Guid cateoryID)
        {
            var n = (from p in WebUtils.db.PRODUCTs where p.CategoryID.Equals(cateoryID) select p).Count();
            if (n > 0)
            {
                return false;
            }

            List<CATEGORY> child = GetChildNode(cateoryID.ToString());
            if (child.Count > 0)
            {
                return false;
            }

            //for (int i = 0; i < child.Count; i++)
            //{
            //    var m = (from p in WebUtils.db.PRODUCTs where p.CategoryID.Equals(child[i].CategoryID) select p).Count();
            //    if (m > 0)
            //    {
            //        return false;
            //    }
            //}

            return true;
        }

        public void Delete(Guid categoryID)
        {
            var category = db.CATEGORies.Single(c => c.CategoryID.Equals(categoryID));
            DB.CATEGORies.DeleteOnSubmit(category);
            DB.SubmitChanges();

            UrlBIZ.GetAllUrl();
        }

        public void OrderUp(Guid idFirst, Guid idLast)
        {
            var last = (from c in WebUtils.db.CATEGORies where c.CategoryID.Equals(idLast) select c).SingleOrDefault();
            var first = (from c in WebUtils.db.CATEGORies where c.CategoryID.Equals(idFirst) select c).SingleOrDefault();

            if (last == null || first == null)
            {
                return;
            }

            int orderFirst = first.Position.Value;
            first.Position = last.Position;
            last.Position = orderFirst;

            WebUtils.db.SubmitChanges();
        }
    }
}
